#include <bits/stdc++.h>
// 2025/02/23
// tag:
// Author: Forever
using namespace std;

using int64 = int64_t;

int n, t, a[200001], maxcow[200001], pos[200001];

void init()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int m = 0, p = 0;
    for (int i = n; i >= 1; i--) {
        if (a[i] >= m) {
            m = a[i];
            p = i;
        }
        maxcow[i] = m;
        pos[i] = p;
    }
}

vector<int> ans(int x, int y, int k = 0)
{
    vector<int> v;
    int p = x - 1;
    while (p < y && maxcow[p + 1] >= k) {
        v.push_back(maxcow[p + 1]);
        p = pos[p + 1];
    }
    return v;
}

vector<int> check(int x, int y, int k)
{
    int m = 0, p = 0;
    for (int i = y - 1; i >= x; i--) {
        if (a[i] >= m) {
            m = a[i];
            p = i;
        }
        maxcow[i] = m;
        pos[i] = p;
    }
    return ans(x, y - 1, k);
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> t;
    for (int i = 1; i <= t; i++) {
        init();
        vector<int> ap = ans(1, n);
        int p = 0, last = 0, cnt = 0;
        while (p < n) {
            last = p;
            p = pos[p + 1];
            vector<int> b = check(last + 1, p, (cnt + 1 == ap.size() ? 0 : ap[cnt + 1]));
            if (b.size()) {
                for (int i = p; i > last + 1; i--)
                    swap(a[i], a[i - 1]);
                int m = 0, p = 0;
                for (int i = n; i >= 1; i--) {
                    if (a[i] >= m) {
                        m = a[i];
                        p = i;
                    }
                    maxcow[i] = m;
                    pos[i] = p;
                }
                ap = ans(1, n);
                break;
            }
            cnt++;
        }
        for (int i = 0; i < ap.size() - 1; i++)
            cout << ap[i] << " ";
        cout << ap[ap.size() - 1];
        cout << endl;
    }
    return 0;
}